package com.example.fasttask;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class DisplayTaskActivity extends Activity {
	boolean noTasksFound = true;
	String time;
	String taskName;
	Task tasky;
	TaskTranslator translator;
	TextView tv;
	String[] tasksColumn = { TaskSQLiteHelper.COLUMN_LENGTH, TaskSQLiteHelper.COLUMN_DATE,
			TaskSQLiteHelper.COLUMN_TASK };
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        translator = new TaskTranslator(this);
        translator.open(); //this is new
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Bundle bundle = getIntent().getExtras();
        time = bundle.getString("time");
        
        Log.d("aoeu", time);
        
        Task t = getTasks(time);
        taskName = t.getName();
        
        setContentView(R.layout.activity_display_task);

        tv = (TextView) findViewById(R.id.newtaskbutton);
        tv.setText(taskName);

    }
	
	public Task getTasks(String time){ 
		Log.d("asdf", "getTasks started");
		//get all tasks with this time
		String [] timearr = time.split(" ");
		
		int timeint = Integer.parseInt(timearr[0]);
		List<Task> tasklist = new ArrayList<Task>();
		Log.d("asdf", translator.allColumns[0]);

		Cursor cursor = translator.database.query(TaskSQLiteHelper.TABLE_TASKS,
				translator.allColumns, TaskSQLiteHelper.COLUMN_LENGTH + " = " +  timeint , null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Task t = translator.cursorToTask(cursor);
			Log.d("asdf", t.getName());
			tasklist.add(t);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();

		Log.d("asdf", tasklist.size() +"");
		if(0 == tasklist.size()){
			noTasksFound();
			return null;
		} else {
			//pick one at random
			int random = (int) (Math.random() * tasklist.size());
			// then get the name of it and display
			Task t = tasklist.get(random);
			tasky = t;
			return t;
		}
	}
	
	public Task getTasks(View v){ 
		Log.d("asdf", "getTasks started");
		//get all tasks with this time
		String [] timearr = time.split(" ");
		
		int timeint = Integer.parseInt(timearr[0]);
		List<Task> tasklist = new ArrayList<Task>();
		Log.d("asdf", translator.allColumns[0]);

		Cursor cursor = translator.database.query(TaskSQLiteHelper.TABLE_TASKS,
				translator.allColumns, TaskSQLiteHelper.COLUMN_LENGTH + " = " +  timeint , null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Task t = translator.cursorToTask(cursor);
			Log.d("asdf", t.getName());
			tasklist.add(t);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();

		Log.d("asdf", tasklist.size() +"");
		if(0 == tasklist.size()){
			noTasksFound();
			return null;
		} else {
			//pick one at random
			int random = (int) (Math.random() * tasklist.size());
			// then get the name of it and display
			Task t = tasklist.get(random);
			tv.setText(t.getName());
			tasky = t;
			return t;
		}
	}
	
	public void deleteTask(View v) {
		translator.deleteTask(tasky);
		getTasks(v);
	}
	
	public void startChooseTimeActivity(View v) {
		finish();
	}
	
	void noTasksFound() {
		final Context context = this;
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		
		alertDialogBuilder.setTitle("No tasks found for " + time);
		
		alertDialogBuilder
			.setMessage("Click OK to go back.")
			.setCancelable(false)
			.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					finish();
				}
			});
		
		AlertDialog alertDialog = alertDialogBuilder.create();
		
		alertDialog.show();
	}
}
